package terra.model.object;

import java.io.Serializable;
import java.util.ArrayList;

import terra.model.object.interfaces.ChangeListener;

public abstract class GObject implements Serializable, ChangeListener {

	private static final long serialVersionUID = -4306591153787720430L;

	private transient ArrayList<ChangeListener> listeners = null;

	public abstract String getName();

	public ArrayList<ChangeListener> getListeners() {
		return listeners;
	}

	public void addListener(ChangeListener object) {
		if (object != null) {
			if (listeners == null) {
				listeners = new ArrayList<ChangeListener>();
				listeners.add(object);
			}
			if (!listeners.contains(object))
				listeners.add(object);
		} else {
			System.out.println("ADD NULL OBJECT  " + object);
		}
	}

	public void removeListener(ChangeListener object) {
		if (object != null) {
			if (listeners == null) {
				listeners = new ArrayList<ChangeListener>();
				return;
			}
			if (listeners.contains(object))
				listeners.remove(object);
		}
	}

	public void listenChanges() {
		if (getListeners() == null)
			return;
		if (getListeners().size() != 0) {
			for (int i = 0; i < getListeners().size(); i++) {
				ChangeListener listener = getListeners().get(i);
				listener.objectChanged(this);
			}
		}
	}

}
